package thread;

// 有三个线程 a b c 让abc同时启动并且cba打印线程名称
//第一种方式让线程sleep来打印
public class ThreadDemo16 {
    public static void main(String[] args) {
        // 创建锁对象 locker 让他们同时竞争该锁
        Object locker = new Object();
        Thread a = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (locker){
                Thread t = Thread.currentThread();
                System.out.print(t.getName()+ " ");
            }
        },"a");

        Thread b = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (locker){
                Thread t = Thread.currentThread();
                System.out.print(t.getName()+ " ");
            }

        },"b");

        Thread c = new Thread(() -> {
            synchronized (locker){
                Thread t = Thread.currentThread();
                System.out.print(t.getName() + " ");

            }
        },"c");

        a.start();
        b.start();
        c.start();

    }
}
